package com.cainaer.shop.controller;


import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.web.controller.BaseController;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.shop.domain.CateType;
import com.cainaer.shop.domain.ShopInfo;
import com.cainaer.shop.domain.ShopLicense;
import com.cainaer.shop.domain.ShopLogin;
import com.cainaer.shop.domain.vo.JudgmentInfoIsNullVo;
import com.cainaer.shop.service.ICateTypeService;
import com.cainaer.shop.service.IShopInfoService;
import com.cainaer.shop.service.IShopLicenseService;
import com.cainaer.shop.service.IShopLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 商家入驻许可证 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-09-03
 */
@RestController
@RequestMapping("/shopLicense/")
public class ShopLicenseController extends BaseController {

    @Autowired
    private IShopLicenseService shopLicenseService;

    @Autowired
    private IShopLoginService shopLoginService;

    private Logger logger = LoggerFactory.getLogger(ShopLicenseController.class);

    @Autowired
    private IShopInfoService shopInfoService;


    @Autowired
    private ICateTypeService cateTypeService;

    /**
     * 判断商家是否已经验证了基本信息
     * Long shopId
     *
     * @param shopId
     * @return
     */
    @GetMapping("judgmentInfoIsNull")
    public AjaxResult judgmentInfoIsNull(Long shopId) {
        if (!StringUtils.isNull(shopId)) {
            ShopLicense shopLicense = shopLicenseService.selectShopLicenseByShopId(shopId);
            logger.info("shopLicense->", shopLicense);
            if (!StringUtils.isNull(shopLicense)) {
                if (StringUtils.isEmpty(shopLicense.getIdCardNum()) || StringUtils.isEmpty(shopLicense.getIdCardImg())) {
                    return AjaxResult.error("身份证未验证");
                } else if (StringUtils.isEmpty(shopLicense.getBusinessNum()) || StringUtils.isEmpty(shopLicense.getBusinessImg())) {
                    return AjaxResult.error("营业执照未验证");
                }
            }

            ShopLogin shopLogin = shopLoginService.selectShopById(shopId);
            if (!StringUtils.isNull(shopLogin)) {
                if (StringUtils.isEmpty(shopLogin.getTel())) {
                    return AjaxResult.error("手机号码未绑定");
                }
            }

            ShopInfo shopInfo = shopInfoService.queryShopInfoByShopId(shopId);
            if (!StringUtils.isNull(shopInfo)) {
                if (StringUtils.isEmpty(shopInfo.getStreet())) {
                    return AjaxResult.error("铺位未设置");
                } else if (StringUtils.isEmpty(shopInfo.getAddress())) {
                    return AjaxResult.error("详细地址未设置");
                } else if (StringUtils.isNull(shopInfo.getCateId())) {
                    return AjaxResult.error("经营类型未设置");
                } else if (StringUtils.isNull(shopInfo.getMarketId())) {
                    return AjaxResult.error("菜市场未设置");
                }
            }


        }
        return AjaxResult.success();
    }

    /**
     * 校验商家是否已经验证了基本信息
     * Long shopId
     *
     * @param shopId
     * @return
     */
    @GetMapping("checkShop")
    public AjaxResult checkShop(Long shopId) {
        if (!StringUtils.isNull(shopId)) {
            ShopLicense shopLicense = shopLicenseService.selectShopLicenseByShopId(shopId);
            ShopLogin shopLogin = shopLoginService.selectShopById(shopId);
            ShopInfo shopInfo = shopInfoService.queryShopInfoByShopId(shopId);
            List<String> cateType = new ArrayList<>();
            if (StringUtils.isNotNull(shopInfo.getCateId())) {
                Matcher strContainData = StringUtils.getStrContainData(shopInfo.getCateId(), "[", "]");
                while (strContainData.find()) {
                    List<String> stringList = StringUtils.stringToList(strContainData.group(1));
                    for (String s : stringList) {
                        String careName = cateTypeService.selectCateTypeById(Long.valueOf(s)).getCareName();
                        if (StringUtils.isNotNull(careName)) {
                            cateType.add(careName);
                        }
                    }
                }
            }
            JudgmentInfoIsNullVo judgmentInfoIsNullVo = new JudgmentInfoIsNullVo(shopLogin, shopInfo, shopLicense, cateType);
            return AjaxResult.success(judgmentInfoIsNullVo);
        }
        return AjaxResult.error("主键不能为空");
    }


    /**
     * 编辑商家入驻许可证信息
     *
     * @param shopLicense
     * @return
     */
    @PostMapping("edit")
    public AjaxResult editShopInfoService(@RequestBody ShopLicense shopLicense) {
        logger.info("shopLicense->{}", shopLicense);
        if (StringUtils.isNull(shopLicense.getShopId())) {
            return AjaxResult.error("主键不能为空");
        }
        return toAjax(shopLicenseService.editShopLicenseByShopId(shopLicense));
    }

    /**
     * 新增商家入驻许可证信息
     *
     * @param shopLicense
     * @return
     */
    @PostMapping("add")
    public AjaxResult insertShopLicense(@RequestBody ShopLicense shopLicense) {
        logger.info("shopLicense->{}", shopLicense);
        if (StringUtils.isNull(shopLicense.getShopId())) {
            return AjaxResult.error("主键不能为空");
        }
        return toAjax(shopLicenseService.insertShopLicense(null));
    }

    /**
     * 校验商家已验证身份
     * 上传身份证
     * 上传营业执照
     * 上传银行卡
     * 如果 许可证数据为空则新增 否则 修改
     *
     * @param shopLicense
     * @return
     */
    @PostMapping("checkShopLicenseUnique")
    public AjaxResult checkShopLicenseUnique(@RequestBody ShopLicense shopLicense) {
        logger.info("shopLicense->{}", shopLicense);
        if (StringUtils.isNull(shopLicense.getShopId())) {
            return AjaxResult.error("主键不能为空");
        }
        ShopLicense selectShopLicenseByShopId = shopLicenseService.selectShopLicenseByShopId(shopLicense.getShopId());
        if (StringUtils.isNull(selectShopLicenseByShopId)) {
            return toAjax(shopLicenseService.insertShopLicense(shopLicense));
        } else {
            return toAjax(shopLicenseService.editShopLicenseByShopId(shopLicense));
        }
    }


}
